package com.pzj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzj.dormitory.entity.Absent;
import com.pzj.dormitory.entity.Dormitory;
import com.pzj.dormitory.entity.Student;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.service.impl.AbsentServiceImpl;
import com.pzj.dormitory.service.impl.BuildingServiceImpl;
import com.pzj.dormitory.service.impl.DormitoryServiceImpl;
import com.pzj.dormitory.service.impl.StudentServiceImpl;
import com.pzj.dormitory.util.ResultVOUtil;
import com.pzj.dormitory.vo.PageVO;
import com.pzj.dormitory.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/absent")
public class AbsentController {

    @Autowired
    private AbsentServiceImpl absentService;
    @Autowired
    private BuildingServiceImpl buildingService;
    @Autowired
    private DormitoryServiceImpl dormitoryService;
    @Autowired
    private StudentServiceImpl studentService;


    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultVO list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize")Integer pageSize){
        return ResultVOUtil.success(absentService.list(pageNum,pageSize));
    }


    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm){
        return ResultVOUtil.success(absentService.search(searchForm));
    }

    @GetMapping("/buildingList")
    public  ResultVO buildingList(){
        return ResultVOUtil.success(buildingService.list());
    }

    @GetMapping("/findDormitoryByBuildingId/{buildingId}")
    public ResultVO findDormitoryByBuildingId(@PathVariable("buildingId") Integer buildingId){
        QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
        dormitoryQueryWrapper.eq("building_id",buildingId);
        return ResultVOUtil.success(dormitoryService.list(dormitoryQueryWrapper));
    }


    @GetMapping("/findStudentByDormitoryId/{dormitoryId}")
    public ResultVO findStudentByDormitoryId(@PathVariable("dormitoryId") Integer dormitoryId){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("dormitory_id",dormitoryId);
        return ResultVOUtil.success(studentService.list(studentQueryWrapper));
    }

    @PostMapping("/save")
    public ResultVO save(@RequestBody Absent absent){
        String createDate = absent.getCreateDate();
        createDate = createDate.substring(0,10);
        absent.setCreateDate(createDate);
        return ResultVOUtil.success(absentService.save(absent));
    }
}

